package com.yunji.app.cabinet;

import com.yunji.aggregate.model.vo.container.CabinetStatisticsVO;
import com.yunji.aggregate.service.ContainerAggregateService;
import com.yunji.cabinet.business.CabinetBusiness;
import com.yunji.cabinet.cache.CabinetCache;
import com.yunji.cabinet.domain.ContainerTypeDO;
import com.yunji.cabinet.dto.CabinetScanCreateDTO;
import com.yunji.cabinet.dto.CapacityCeilingDTO;
import com.yunji.cabinet.dto.SetProductDTO;
import com.yunji.cabinet.dto.device.CabinetCargoInfoDTO;
import com.yunji.cabinet.service.impl.CabinetBaseService;
import com.yunji.cabinet.vo.ContainerProductVO;
import com.yunji.common.base.BaseController;
import com.yunji.common.base.response.Response;
import com.yunji.common.base.response.ResponseHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * @Project: yunji-coffee
 * @Package com.yunji.web.cabinet
 * @FileName GateWayCabinetController.java
 * @Copyright Copyright © 2024 云集互联. All Rights Reserved
 * @Company com
 * @Version 1.0
 * @Since JDK1.8.0_201
 * @CreateTime 2024-06-10 22:42:41
 * @Description 货柜信息
 * @Modification History
 * Author					History Time			Version			Description
 * -------------------------------------------------------------------------------
 * HuaAo0803@163.com	2024-06-10 22:42:41		1.0				1.0
 */
@RestController
@Api(tags = "移动端-货柜信息")
@RequestMapping("/gateway/container")
public class AppCabinetController extends BaseController {

    @Resource
    CabinetBusiness cabinetBusiness;

    @Resource
    ContainerAggregateService aggregateService;

    @Resource
    CabinetBaseService baseService;

    @DeleteMapping("/delete/{ids}")
    @ApiOperation("app删除货柜信息")
    public Response<String> delete(@PathVariable Long... ids) {
        cabinetBusiness.delete(ids);
        return ResponseHelper.success("删除成功");
    }

    @ApiOperation("查看货柜类型code")
    @GetMapping("/getContainerType/{qrCode}")
    public Response<String> getContainerType(@PathVariable("qrCode") String code) {
        return ResponseHelper.success(CabinetCache.getTypeCode(cabinetBusiness.getImeiByQrcode(code)));
    }

    @ApiOperation("货柜类型列表")
    @GetMapping("/containerType/list")
    public Response<List<ContainerTypeDO>> getContainerTypeList() {
        List<ContainerTypeDO> list = baseService.typeList();
        return ResponseHelper.successWithPage(list);
    }

    @ApiOperation("查看货柜和多个商品以及代理价格")
    @GetMapping("/listContainerProduct/{qrCode}")
    public Response<ContainerProductVO> listContainerProduct(@PathVariable("qrCode") String code) {
        return ResponseHelper.success(cabinetBusiness.getContainerProductByQr(code));
    }

    @ApiOperation("员工根据二维码获取货柜信息")
    @GetMapping("/{qrCode}")
    public Response<CabinetStatisticsVO> container(@PathVariable("qrCode") String code) {
        CabinetStatisticsVO cabinet = aggregateService.selectContainerByCode(code);
        return ResponseHelper.success(cabinet);
    }

    @ApiOperation("用户扫码记录添加")
    @PutMapping("/createScanMsg")
    public Response<?> createScanMsg(@RequestBody @Valid CabinetScanCreateDTO dto) {
        return ResponseHelper.handle(cabinetBusiness.createScanMsg(dto), "扫码记录统计失败").withDataId(dto.getScanCode());
    }

    @ApiOperation("员工端删除货柜")
    @DeleteMapping("/{qrCode}")
    public Response<String> delete(@PathVariable("qrCode") String code) {
        cabinetBusiness.deleteByCode(code);
        return ResponseHelper.success("删除成功");
    }

    @ApiOperation("获取容量上限")
    @GetMapping("/getCapacityCeiling")
    public Response<Integer> getCapacityCeiling(CapacityCeilingDTO dto) {
        return ResponseHelper.success(cabinetBusiness.getCapacityCeiling(dto));
    }
    @ApiOperation("更换商品")
    @PostMapping("/changeProduct")
    public Response<String> changeProduct(@RequestBody SetProductDTO dto){
        cabinetBusiness.changeProduct(dto);
        return ResponseHelper.success("更换成功");
    }

    @ApiOperation("检测电压/电量")
    @GetMapping("/checkVolt/{qrcode}")
    public Response<?> checkVolt(@PathVariable("qrcode") String qrcode) {
        return ResponseHelper.success(cabinetBusiness.checkVolt(qrcode));
    }

    @ApiOperation("获取检测数量")
    @GetMapping("/monitorQuantity")
    public Response<List<CabinetCargoInfoDTO>> monitorQuantity(@RequestParam("maintainCode") String maintainCode,@RequestParam("imei") String imei) {
        return ResponseHelper.success(cabinetBusiness.monitorQuantity(maintainCode,imei));
    }
}